import { ref, onMounted, onUnmounted } from "vue"

export const useTime = () => {
    const curTime = ref("0000-00-00 00:00:00")
    let crob

    const getCurDate = () => {
        var date = new Date()
        var y = date.getFullYear()
        var m = date.getMonth() + 1
        var d = date.getDate()
        var h = date.getHours()
        var min = date.getMinutes()
        var s = date.getSeconds()
        s = s < 10 ? "0" + s : s
        curTime.value = y + "-" + m + "-" + d + " " + h + ":" + min + ":" + s
    }

    onMounted(() => {
        getCurDate()
        crob = setInterval(getCurDate, 1000)
    })

    onUnmounted(() => {
        if (crob) clearInterval(crob)
    })

    return curTime
}
